package com.dolphin.servicemap.remot;

import com.dolphin.common.constant.AmapConfigConstants;
import com.dolphin.common.dto.ResponseResult;
import com.dolphin.common.dto.TrsearchResponse;
import com.dolphin.common.response.TerminalResponse;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class TerminalClient {
    @Value("${amap.key}")
    private String amapKey;
    @Value("${amap.sid}")
    private String amapSid;

    @Autowired
    RestTemplate restTemplate;
    public ResponseResult add(String name,String desc){
        StringBuilder url = new StringBuilder();

        url.append(AmapConfigConstants.TERMINAL_ADD_URL);
        url.append("?key=" + amapKey);
        url.append("&");
        url.append("sid=" + amapSid);
        url.append("&");
        url.append("name=" + name);
        url.append("&");
        url.append("desc=" + desc);
        log.info("创建终端请求："+url.toString());
        ResponseEntity<String> response = restTemplate.postForEntity(url.toString(), null, String.class);
        log.info("创建终端响应："+response.getBody());
        String body = response.getBody();
        JSONObject result = JSONObject.fromObject(body);
        JSONObject data = result.getJSONObject("data");
        String tid = data.getString("tid");
        TerminalResponse terminalResponse = new TerminalResponse();
        terminalResponse.setTid(tid);



        return ResponseResult.success(terminalResponse);
    }

    public ResponseResult<List<TerminalResponse>> aroundSearch(String center, Integer radius) {
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.TERMINAL_AROUNDSEARCH_URL);
        url.append("?key=" + amapKey);
        url.append("&");
        url.append("sid=" + amapSid);
        url.append("&");
        url.append("center=" + center);
        url.append("&");
        url.append("radius=" + radius);
        log.info("终端AroundSearch请求："+url.toString());
        ResponseEntity<String> response = restTemplate.postForEntity(url.toString(), null, String.class);
        log.info("终端AroundSearch响应："+response.getBody());
        String body = response.getBody();
        JSONObject result = JSONObject.fromObject(body);
        JSONObject data = result.getJSONObject("data");
        JSONArray results = data.getJSONArray("results");

        List<TerminalResponse> terminalResponseList = new ArrayList<>();
        for (int i = 0; i < results.size(); i++) {
            TerminalResponse terminalResponse = new TerminalResponse();

            JSONObject jsonObject = results.getJSONObject(i);
            //desc 是carId
            String desc = jsonObject.getString("desc");
            Long carId = Long.parseLong(desc);
            String tid = jsonObject.getString("tid");

            JSONObject location = jsonObject.getJSONObject("location");
            String longitude = location.getString("longitude");
            String latitude = location.getString("latitude");

            terminalResponse.setTid(tid);
            terminalResponse.setCarId(carId);
            terminalResponse.setLongitude(longitude);
            terminalResponse.setLatitude(latitude);
            terminalResponseList.add(terminalResponse);


        }

        return ResponseResult.success(terminalResponseList);
    }

    public ResponseResult<TrsearchResponse> trsearch(String tid, Long startTime, Long endTime) {
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.TERMINAL_TRSEARCH_URL);
        url.append("?key=" + amapKey);
        url.append("&");
        url.append("sid=" + amapSid);
        url.append("&");
        url.append("tid=" + tid);//1276607483
        url.append("&");
        url.append("starttime=" + startTime);//1746454563214
        url.append("&");
        url.append("endtime=" + endTime);//1746454621903
        log.info("终端轨迹请求："+url.toString());
        ResponseEntity<String> forEntity = restTemplate.postForEntity(url.toString(), null, String.class);
        log.info("终端轨迹响应："+forEntity.getBody());

        String body = forEntity.getBody();
        JSONObject result = JSONObject.fromObject(body);
        JSONObject data = result.getJSONObject("data");
        int counts = data.getInt("counts");
        if (counts == 0) {
            return null;
        }
        JSONArray tracks = data.getJSONArray("tracks");
        long driverMile = 0L;
        long driverTime = 0L;
        for (int i = 0; i < tracks.size(); i++) {
            JSONObject jsonObject = tracks.getJSONObject(i);

            long distance = jsonObject.getLong("distance");
            driverMile  = driverMile + distance;

            long time = jsonObject.getLong("time");
            driverTime = driverTime + time;


        }
        TrsearchResponse trsearchResponse = new TrsearchResponse();
        trsearchResponse.setDriverTime(driverTime/(1000*60));
        trsearchResponse.setDriverMile(driverMile);
        return ResponseResult.success(trsearchResponse);
    }
}
